package com.tanhua.app.service;

import com.alibaba.fastjson.JSON;
import com.tanhua.api.db.QuestionApi;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.RecommendUserApi;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.vo.PageBeanVo;
import com.tanhua.domain.vo.TodayBest;
import com.tanhua.template.HuanXinTemplate;
import org.apache.commons.lang3.RandomUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RecommendService {

    @Reference
    private RecommendUserApi recommendUserApi;

    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private QuestionApi questionApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    //查询数据
    public ResponseEntity findMaxScore() {
        // 获取登录用户的id
        User user = UserHolder.getUser();
        // 获取到缘分值最高的女生信息
        RecommendUser recommendUser = recommendUserApi.findMaxScore(user.getId());

        //登录用户没有 做个固定假的
        if(recommendUser==null){
            recommendUser=new RecommendUser();
            recommendUser.setUserId(2L);
            recommendUser.setScore(99D);
        }

        // 根据最高女生的信息id去mysql中查询到 该人的头像 昵称 等
        Long infoId = recommendUser.getUserId();
        UserInfo userInfo = userInfoApi.findUserInfoVo(infoId);

        //此时：app要什么数据 我们就封装什么数据
        TodayBest todayBest = new TodayBest();
        BeanUtils.copyProperties(userInfo,todayBest);
        todayBest.setTags(userInfo.getTags().split(","));
        todayBest.setFateValue(recommendUser.getScore().longValue());

        return ResponseEntity.ok(todayBest);
    }

    //查询多个推荐佳人
    public ResponseEntity findScorePage(Integer page, Integer pagesize) {
        // 1 获取当前用户
        User user = UserHolder.getUser();
        //2 分页查询多个佳人信息
        PageBeanVo pb = recommendUserApi.findScorePage(user.getId(), page, pagesize);
        //3 拿着每个佳人信息去查userInfo(头像)
        List<RecommendUser> RecommendUserList = (List<RecommendUser>) pb.getItems();

        //如果items没有数据 做个固定假的
        if(RecommendUserList.size()==0){
            RecommendUserList=formtuijianren();
        }

        // 如果items有数据
        List<TodayBest> todayBestList = new ArrayList<>();
        for (RecommendUser recommendUser : RecommendUserList) {
            //4 将每个佳人信息都封装给List<TodayBest>
            TodayBest todayBest = new TodayBest();
            //每个佳人的头像 昵称
            UserInfo userInfo = userInfoApi.findUserInfoVo(recommendUser.getUserId());
            BeanUtils.copyProperties(userInfo,todayBest);
            todayBest.setTags(userInfo.getTags().split(","));
            todayBest.setFateValue(recommendUser.getScore().longValue());
            todayBestList.add(todayBest);
        }


        //5 将封装好的List<TodayBest>  覆盖pageBean List<RecommendUser>
        pb.setItems(todayBestList);
        return ResponseEntity.ok(pb);
    }

    //自己造数据
    private List<RecommendUser> formtuijianren() {
        List<RecommendUser> list = new ArrayList<>();

        for(Long i=1L;i<=10;i++){
            RecommendUser recommendUser=new RecommendUser();
            recommendUser.setUserId(i);
            recommendUser.setScore(RandomUtils.nextDouble(66,99));
            list.add(recommendUser);
        }
        return list;
    }

    //查询佳人的个人详情
    public ResponseEntity personalInfo(Long jrid) {

        // 1 查询佳人信息
        UserInfo userInfo = userInfoApi.findUserInfoVo(jrid);
        // 2 当前登录用户和佳人之间的缘分值
        Long score=recommendUserApi.findScore(UserHolder.getUser().getId(),jrid);
        // 3 封装TodayBest
        TodayBest todayBest = new TodayBest();
        BeanUtils.copyProperties(userInfo,todayBest);
        todayBest.setTags(userInfo.getTags().split(","));
        todayBest.setFateValue(score);
        //4 返回
       return  ResponseEntity.ok(todayBest);

    }

    //查询佳人的问题
    public ResponseEntity strangerQuestions(Long jrid) {
        Question question = questionApi.findUserStr(jrid);
        if(question==null){
            return ResponseEntity.ok("你是GG还是MM啊?");
        }
        return ResponseEntity.ok(question.getStrangerQuestion());
    }

    // 回复陌生人问题
    public ResponseEntity toStrangerQuestions(Integer toUserId, String reply) {
        Map<String,String> map=new HashMap<>();
        // 组装数据
        map.put("userId",UserHolder.getUser().getId().toString()); //1 谁回复的
        //查询
        UserInfo userInfo = userInfoApi.findUserInfoVo(UserHolder.getUser().getId());
        map.put("nickname",userInfo.getNickname());//2 回复人的昵称
        ResponseEntity responseEntity = strangerQuestions(toUserId.longValue());
        String question =(String)responseEntity.getBody();
        map.put("strangerQuestion",question); //3 陌生人的问题
        map.put("reply",reply); // 4 问题的答案
        //进行发送
        //map--String(json)
        String json = JSON.toJSONString(map);
        System.out.println(json); //看效果
        huanXinTemplate.sendMsg(toUserId.toString(),json);

        return ResponseEntity.ok(null);
    }
}
